package org.devgframwork.es5.elasticserach;

import com.google.gson.GsonBuilder;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @auther dh
 * @date 2021/12/23 16:43
 */
@Slf4j
@Component
public class EsConfig implements InitializingBean {

	@Value("${devg.es5.serverUri:http://127.0.0.1:9200}")
	private String serverUri;

	@Value("${devg.es5.showLog:false}")
	private Boolean showLog;

	@Override
	public void afterPropertiesSet() throws Exception {
		initEsConfig();
		EsSearch.setShowLog(showLog);
	}

	private void initEsConfig() {
		synchronized (EsConfig.class) {
			JestClientFactory factory = new JestClientFactory();
			factory.setHttpClientConfig(new HttpClientConfig.Builder(serverUri)
					.gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create())
					.multiThreaded(true)
					.readTimeout(10000)
					.defaultMaxTotalConnectionPerRoute(1000)
					.maxTotalConnection(10000)
					.build());
			EsSearch.jestClient = factory.getObject();
			log.info("init EsSearch jestClient success ... ");
		}
	}

}
